import asyncio
import re

import aiohttp

UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'

FAVOR_URLS = [
    'https://jd.com/',
    'https://www.taobao.com/',
    'https://www.python.org/',
    'https://github.com/',
    'https://www.sohu.com/',
    'http://www.mobiletrain.org/',
    'https://xclient.info/',
    'https://www.bilibili.com/',
    'https://www.douban.com/',
]
# 正则表达式命名捕获组（具名捕获组）
TITLE_PATTERN = re.compile(r'\<title\>(?P<foo>.*?)\<\/title\>')


async def fetch_title(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url, headers={'User-Agent': UA}, ssl=False) as resp:
            page_code = await resp.text()
            match = TITLE_PATTERN.search(page_code)
            if match:
                print(match.group('foo'))


cos_list = [fetch_title(url) for url in FAVOR_URLS]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(cos_list))
loop.close()
